<template>
  <div class="page">
    <!-- 头部 -->
    <div class="header">
      <div class="header-box">
        <div class="h-btn nav" @click="showNav">
          <qm-icon name="icon-caidan"></qm-icon>
        </div>
        <div class="logo">
          <a href="/"
            ><img src="@/assets/img/layout/logo.png" alt=" qiuMo"
          /></a>
        </div>
        <div class="h-btn search"></div>
        <!-- <div class="h-btn search" @click="themeChange">
          <qm-icon
            :name="theme == 'default' ? 'icon-yueliang' : 'icon-taiyang'"
          ></qm-icon>
        </div> -->
        <!-- <div class="h-btn search" @click="themeChange">
          <img
            v-if="qq"
            :src="'http://q2.qlogo.cn/headimg_dl?dst_uin=' + qq + '&spec=100'"
            alt=""
          />
          <img
            v-else
            src="http://q2.qlogo.cn/headimg_dl?dst_uin=649980562&spec=100"
            alt=""
          />
        </div> -->
      </div>
    </div>
    <!-- 侧边导航 -->
    <div class="mobile-header">
      <div
        class="navbar visible-xs-block scroll-y mini-scrollbar left"
        :class="{ show: show }"
        touch-direction="left"
      >
        <!-- 菜单 -->
        <ul class="mobile-menus">
          <!-- 顶级菜单 -->
          <li class="menus-li" v-for="(item, index) in menus" :key="index">
            <a
              class="top-a"
              @click="openUrl(item)"
              :class="{ 'font-col': url == item.path }"
            >
              <qm-icon class="aicon" :name="item.icon"></qm-icon>&nbsp;
              {{ item.name }}
            </a>
            <!-- 二级菜单 -->
            <ul class="sub-menu" v-if="item.hasChild">
              <li class="sub-item" v-for="(child, i) in item.children" :key="i">
                <a
                  @click="openUrl(child)"
                  :class="{ 'font-col': url == child.path }"
                >
                  <qm-icon class="aicon" :name="child.icon"></qm-icon>&nbsp;
                  {{ child.name }}
                </a>
              </li>
            </ul>
          </li>
          <!-- 下面的菜单需要登录才能显示 部分菜单需要有指定角色 -->
          <li class="menus-li" v-if="hasAdmin">
            <a class="top-a">
              <qm-icon class="aicon" name="icon-siyecao-copy"></qm-icon>&nbsp;
              后台管理
            </a>
            <!-- 二级菜单 -->
            <ul class="sub-menu">
              <li class="sub-item" v-if="hasSuperAdmin">
                <a
                  @click="openUrl({ type: 1, path: '/admin/user' })"
                  :class="{ 'font-col': url == '/admin/user' }"
                >
                  <qm-icon class="aicon" name="icon-shortcut-fill"></qm-icon
                  >&nbsp; 用户管理
                </a>
              </li>
              <li class="sub-item" v-if="hasAdmin">
                <a
                  @click="openUrl({ type: 1, path: '/admin/auth' })"
                  :class="{ 'font-col': url == '/admin/auth' }"
                >
                  <qm-icon class="aicon" name="icon-quanxian"></qm-icon>&nbsp;
                  权限管理
                </a>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <div
        class="fixed-body"
        :class="{ 'fix-show': show }"
        data-close=".mobile-navbar"
        @click="showNav"
      ></div>
    </div>
    <!-- 主体内容 -->
    <div class="body">
      <router-view v-slot="{ Component }">
        <keep-alive>
          <component
            :is="Component"
            :key="$route.name"
            v-if="$route.meta.keepAlive"
          />
        </keep-alive>
        <component
          :is="Component"
          :key="$route.name"
          v-if="!$route.meta.keepAlive"
        />
      </router-view>
    </div>
    <!-- 底部信息 -->
    <footer class="footer">
      <div class="container-footer">
        <!-- <p class="fcode-links">
          <a>友链申请</a>
          <a>免责声明</a>
          <a>广告合作</a>
          <a>关于我们</a>
        </p> -->
        <div class="footer-muted">
          Copyright ©&nbsp;2023&nbsp;·&nbsp;
          <a>QiuMo</a>.
        </div>
      </div>
    </footer>
    <back-top :scrollmyself="true"></back-top>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import menusJson from '@/menu/index.json'
import qmIcon from '@/components/qm-icon.vue'
import { bus } from '@/utils/bus'
export default {
  name: 'index-layout',
  data() {
    let config = []
    Object.assign(config, menusJson)
    return {
      show: false,
      theme: 'default',
      url: '/home',
      menus: menusJson,
      hasAdmin: false,
      hasSuperAdmin: false
    }
  },
  computed: {
    isLogin() {
      return this.$store.getters.isLogin
    },
    role() {
      return this.$store.getters.role
    },
    qq() {
      return this.$store.getters.qq
    }
  },
  components: { qmIcon },
  created() {
    let _this = this
    // window.addEventListener('setItemEvent', function (e) {
    //   if (e.key === 'theme') {
    //     _this.theme = e.newValue
    //   }
    //   if (e.key === 'activeUrl') {
    //     _this.url = e.newValue
    //   }
    // })
    bus.$on('toPage', (value) => {
      _this.url = value
    })
  },
  mounted() {
    this.init() // 调用初始化主题方法
    this.setStatus()
  },
  methods: {
    // 将存在 vuex 中的更改主题方法拿过来
    ...mapMutations('theme', ['setThemeStore']),
    // 主题初始化
    init() {
      this.setThemeStore(this.$store.state.theme.theme)
      this.theme = localStorage.getItem('theme')
      this.url = localStorage.getItem('activeUrl')
    },
    //主题切换
    themeChange() {
      this.theme = this.theme == 'default' ? 'dark' : 'default'
      this.setThemeStore(this.theme)
    },
    //一些状态数据初始化
    setStatus() {
      if (!this.isLogin) {
        this.hasAdmin = false
        this.hasSuperAdmin = false
      } else {
        if (this.role == 'ADMIN') {
          this.hasAdmin = true
          this.hasSuperAdmin = false
        } else if (this.role == 'SUPERADMIN') {
          this.hasAdmin = true
          this.hasSuperAdmin = true
        }
      }
    },
    showNav() {
      this.show = !this.show
    },
    openUrl(item) {
      if (item.type == '1') {
        this.show = !this.show
        this.$router.push(item.path)
      }
      if (item.type == '2') {
        this.show = !this.show
        window.open(item.path, '_blank')
      }
    }
  }
}
</script>

<style scoped lang="less">
.page {
  padding-top: 72px;
}

.header {
  position: fixed;
  top: 0;
  z-index: 999;
  padding: 0 8px;
  margin-bottom: 20px;
  width: 375px;
  height: 52px;
  background: @hbg;
  transition: 0.3s;
  backdrop-filter: saturate(5) blur(20px);

  .header-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 52px;

    .logo {
      width: 175px;
      height: 36px;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .h-btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 40px;
      height: 40px;

      img {
        width: 40px;
        height: 40px;
        border-radius: 40px;
      }
    }
  }
}

.font-col {
  pointer-events: none;
  color: @themecolor !important;
}

.mobile-header {
  .navbar {
    position: fixed;
    top: 0;
    bottom: 0;
    z-index: 997;
    padding: 70px 20px;
    width: 281px;
    background: @fbg;
    transition: transform 0.5s;

    .mobile-menus {
      padding: 8px;
      background: rgb(0 0 0 / 3%);
      border-radius: 5px;

      .aicon {
        display: inline-block !important;
        width: 15px;
        height: 15px;
      }

      .menus-li {
        .top-a {
          display: block;
          overflow: hidden;
          padding: 6px 15px;
          height: 32px;
          font-size: 14px;
          text-overflow: ellipsis;
          white-space: nowrap;
          color: @iconc;

          &::before {
            display: inline-block;
            margin: 0;
            margin-right: 12px;
            width: 0.3em;
            height: 0.3em;
            border: 0.1em solid @themecolor;
            border-radius: 1em;
            content: '';
            vertical-align: 0.1em;
          }
        }

        .sub-menu {
          padding: 0 10px;
          word-wrap: break-word;
          white-space: normal;

          .sub-item {
            display: inline-block;
            width: calc(50% - 6px);
            text-align: center;

            a {
              position: relative;
              display: block;
              overflow: hidden;
              padding: 5px;
              margin: 2px;
              font-size: 0.9em;
              text-overflow: ellipsis;
              white-space: nowrap;
              color: @iconc;
              background: @main-border-color;
              border-radius: 4px;
            }
          }
        }
      }
    }
  }

  .visible-xs-block {
    display: block !important;
  }

  .scroll-y {
    overflow-x: hidden;
    overflow-y: auto;
  }

  .mini-scrollbar {
    scrollbar-width: none;
  }

  .left {
    left: -2px;
    transform: translateX(-110%);
  }

  .show {
    transition: transform 0.6s cubic-bezier(0.28, 0.9, 0.34, 0.99);
    transform: translate(0);
  }

  .fixed-body {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: 996;
    width: 100%;
    height: 100%;
    background: rgb(24 25 27 / 80%);
    opacity: 0;
    visibility: hidden;
    transition: 00.3s;
  }

  .fix-show {
    opacity: 1;
    visibility: unset;
  }
}

.body {
  min-height: calc(100vh - 72px - 50px);
}

.footer {
  // padding: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 15px;
  width: 375px;
  z-index: 990;
  height: 35px;
  font-size: 12px;
  color: #999;
  background: @fbg;

  a {
    color: #999;
  }

  a + a::before {
    display: inline-block;
    margin: 0 10px;
    width: 4px;
    height: 4px;
    content: '';
  }

  .container-footer {
    width: 100%;

    .fcode-links {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 10px;
    }

    .footer-muted {
      font-size: 12px;
      text-align: center;
    }
  }
}
</style>
